<template>
  <div class="menu">
    <el-button
      type="primary"
      size="small"
      @click="addModify({ popTitle: '新增', menuInfo: {} })"
      >添加菜单</el-button
    >
    <div class="tree-title">
      <div class="title flex1">菜单</div>
      <div class="cont flex0">
        <div class="icon bor">图标</div>
        <div class="sort bor">排序</div>
        <div class="url bor">路由路劲</div>
        <div class="operation bor">操作</div>
      </div>
    </div>
    <el-tree :data="menu_data" accordion>
      <template v-slot="{ data }">
        <div class="tree-row flex1">
          <div class="flex0 title-name">{{ data.menu_name }}</div>
          <div class="cont">
            <div class="icon bor">
              <i class="iconfont" :class="data.menu_icon"></i>
            </div>
            <div class="router-sort bor">{{ data.menu_sort }}</div>
            <div class="router-url bor" :title="data.path_url">
              {{ data.path_url }}
            </div>
            <div class="operation bor">
              <div v-if="!data.children || data.children.length === 0">
                <el-button
                  type="success"
                  size="mini"
                  @click="apiAuthority(data.menu_id)"
                >
                  权限
                </el-button>
                <el-button
                  type="warning"
                  size="mini"
                  v-if="data.label !== '首页'"
                  @click="
                    addModify({ popTitle: '修改', menuInfo: { ...data } })
                  "
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  size="mini"
                  v-if="data.label !== '首页'"
                >
                  删除
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-tree>
    <!-- 新增，修改弹窗 -->
    <el-dialog
      :title="popTitle + '菜单'"
      :visible.sync="addModifyPop"
      width="30%"
      :before-close="handleClose"
    >
      <MenuPop v-if="addModifyPop" :menuInfo="menuInfo"></MenuPop>
    </el-dialog>
    <!-- 接口权限弹框 -->
    <el-dialog
      title="接口权限"
      :visible.sync="apiAuthorityPop"
      width="50%"
      :before-close="handleClose"
    >
      <ApiAuthorityPop></ApiAuthorityPop>
    </el-dialog>
  </div>
</template>
<script>
import MenuPop from "./components/menu-pop"
import ApiAuthorityPop from "./components/api-authority-pop"
import { menuJson } from "./menuJson"
export default {
  components: {
    MenuPop,
    ApiAuthorityPop,
  },
  data() {
    return {
      menu_data: menuJson, //菜单
      addModifyPop: false, //新增，修改弹框
      apiAuthorityPop: false, //接口权限弹框
      popTitle: "", //新增修改弹框标题
      menuInfo: {}, //修改时传子组件
    }
  },
  methods: {
    // 新增,修改
    addModify(obj) {
      this.popTitle = obj.popTitle
      this.menuInfo = obj.menuInfo
      this.addModifyPop = true
    },
    // 权限
    apiAuthority(id) {
      console.log(id)
      this.apiAuthorityPop = true
    },
    handleClose(done) {
      done()
    },
  },
}
</script>
<style scoped lang="scss">
.menu {
  /deep/.el-tree-node__content {
    height: 40px;
    border-bottom: 1px #dddddd solid;
  }
  .tree-title {
    min-width: 1000px;
    border-bottom: 1px #dddddd solid;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #ffffff;
    margin-top: 20px;
    font-size: 14px;
    .title {
      display: flex;
      justify-content: center;
    }
    .cont {
      display: flex;
      flex-direction: row;
      .bor {
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        box-sizing: border-box;
      }
      .icon {
        width: 80px;
      }
      .sort {
        width: 80px;
      }
      .url {
        width: 500px;
      }
      .operation {
        width: 200px;
      }
    }
  }
  .tree-row {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    .title-name {
      padding-right: 20px;
    }
    .cont {
      display: flex;
      flex-direction: row;
      .bor {
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        box-sizing: border-box;
      }
      .icon {
        width: 80px;
        image {
          width: 24px;
          height: auto;
        }
      }
      .router-sort {
        width: 80px;
      }
      .router-url {
        width: 500px;
      }
      .operation {
        width: 200px;
      }
    }
  }
}
.el-tree {
  min-width: 1000px;
}
</style>
